package com.fudan.backend.dao;

import com.fudan.backend.handler.IdTypeHandler;
import com.fudan.backend.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface OrderDao {
    @Results(id = "orderResultMap", value = {
            @Result(column = "order_id", property = "orderId", typeHandler = IdTypeHandler.class),
            @Result(column = "user_id", property = "userId", typeHandler = IdTypeHandler.class),
            @Result(column = "store_id", property = "storeId", typeHandler = IdTypeHandler.class),
            @Result(column = "bill_id", property = "billId", typeHandler = IdTypeHandler.class),
            @Result(column = "address_id", property = "addressId", typeHandler = IdTypeHandler.class),
            @Result(column = "activity_id", property = "activityId", typeHandler = IdTypeHandler.class),
            @Result(property = "orderItemList", column = "order_id", typeHandler = IdTypeHandler.class, javaType = List.class,
                    many = @Many(select = "com.fudan.backend.dao.OrderItemDao.selectOrderItemsByOrderId"))
    })
    @Select("select * from t_order where order_id = #{orderId} and is_deleted = 0")
    Order selectOrderByOrderId(String orderId);

    @ResultMap("orderResultMap")
    @Select("select * from t_order where order_id = #{orderId}")
    Order selectOrderByOrderIdNotCheckDelete(String orderId);

    @ResultMap("orderResultMap")
    @Select("select * from t_order " +
            "where user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler} " +
            "and is_deleted = 0")
    List<Order> selectOrderByUserId(String userId);

    @ResultMap("orderResultMap")
    @Select("select * from t_order " +
            "where user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler} " +
            "and status = #{status} " +
            "and is_deleted = 0")
    List<Order> selectOrderByUserIdAndStatus(String userId, int status);

    @ResultMap("orderResultMap")
    @Select("select * from t_order " +
            "where user_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler} " +
            "and status = #{status} " +
            "and is_deleted = 0")
    List<Order> selectOrderByStoreIdAndStatus(String storeId, int status);

    @ResultMap("orderResultMap")
    @Select("select * from t_order " +
            "where user_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler} " +
            "and status = #{status} " +
            "and create_time > #{startTime} " +
            "and create_time < #{endTime} ")
    List<Order> selectOrderByStoreIdAndTimeAndStatus(String storeId, Date startTime, Date endTime, int status);

    @ResultMap("orderResultMap")
    @Select("<script>" +
            "select * from t_order " +
            "where is_deleted = 0 " +
            "and order_id in " +
            "<foreach item='id' collection='idList' open='(' close=')' separator=','>" +
            "#{id, typeHandler=com.fudan.backend.handler.IdTypeHandler}" +
            "</foreach>" +
            "</script>")
    List<Order> selectOrdersByOrderIdList(List<String> idList);

    @Select("select COUNT(*) from t_order where " +
            "store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler} and status = 3")
    int selectOrderCountByStoreId(String storeId);

    @Update("update t_order set " +
            "user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, create_time = #{createTime}, " +
            "pay_time = #{payTime}, delivery_time = #{deliveryTime}, take_time = #{takeTime}, " +
            "bill_id = #{billId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "address_id = #{addressId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "activity_id = #{activityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "payment = #{payment}, allowance = #{allowance}, status = #{status}, more_info = #{moreInfo} " +
            "where order_id = #{orderId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    int updateOrder(Order order);

    @Update("update t_order set " +
            "status = #{status} " +
            "where order_id = #{orderId}")
    int setOrderStatus(String orderId, int status);

    /**
     * 将创建时间超过date的订单取消
     */
    @Update("update t_order set " +
            "status = 4 " +
            "where create_time < #{date} " +
            "and status = 0")
    int setOvertimeOrderStatus(Date date);

    @Insert("insert into t_order " +
            "(order_id, user_id, store_id, create_time, pay_time, delivery_time, take_time, address_id, activity_id, payment, allowance, status, more_info)" +
            " values " +
            "(#{orderId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{createTime}, " +
            "#{payTime}, " +
            "#{deliveryTime}, " +
            "#{takeTime}, " +
            "#{addressId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{activityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{payment}, " +
            "#{allowance}, " +
            "#{status}, " +
            "#{moreInfo})")
    int insertOrder(Order order);

    @Update("update t_order set " +
            "is_deleted = 1 " +
            "where order_id = #{orderId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    int deleteOrderByOrderId(String orderId);
}
